package com.cm.h2.web;

import com.cm.h2.dao.UserDao;
import com.cm.h2.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ki on 2018/9/22.
 */
@RestController
public class UserController {
    @Autowired
    private UserDao userDao;


    /**
     * 预热后，批量插入 1W条数据 耗时100 - 250 毫秒
     * @return
     */
    @GetMapping("saveAll")
    public String saveAll(){
        long startTime=System.currentTimeMillis();
        List<User> list= new ArrayList<>();
        for(int i=0;i<10000;i++){
            list.add(new User("小明-"+i,i));
        }
        userDao.saveAll(list);
        System.out.println(System.currentTimeMillis()-startTime);
        return "ok";
    }

    /**
     * 预热之后300-400毫秒 进行1000次单次插入数据
     * @return
     */
    @GetMapping("saveOne1K")
    public String saveOne1K(){
        long startTime=System.currentTimeMillis();
        User u1 = null;
        for(int i=0;i<1000;i++){
            u1=new User("小明-"+i,19);
            userDao.save(u1);
        }
        System.out.println(System.currentTimeMillis()-startTime);
        return "ok";
    }

    @GetMapping("getListAll")
    public List<User> getListAll(){
        return (List<User>) userDao.findAll();
    }

    @GetMapping("getUserByName")
    public User getUserByName(String name){
        return userDao.findByName(name);
    }

    @GetMapping("count")
    public long count(){
        return userDao.count();
    }
}
